home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 15 code / Floating Windows / Floating Windows Code / Shell Code / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-17  |  8.1 KB  |  291 lines  |  [TEXT/MMCC]

  1. /*    Application skeleton by Dean Yu    */
  2. /*    Main routine and initialization routines                    */
  3.  
  4. // If the interfaces aren't included yet, do the standard includes. Otherwise, precompiled headers
  5. // have been loaded already, thank you very much
  6. #ifndef __TYPES__
  7.     #include <Types.h>
  8.     #include <AppleEvents.h>
  9.     #include <Desk.h>
  10.     #include <Dialogs.h>
  11.     #include <Editions.h>
  12.     #include <Events.h>
  13.     #include <Fonts.h>
  14.     #include <Menus.h>
  15.     #include <SegLoad.h>
  16.     #include <TextUtils.h>
  17.     #include <ToolUtils.h>
  18.     #include <Windows.h>
  19. #endif
  20.  
  21. // Not in Metrowerks default precompiled headers
  22. #include <GestaltEqu.h>
  23.  
  24. #include "FloaterSample.h"
  25. #include "appleEventHandlers.h"
  26. #include "eventHandler.h"
  27. #include "menuDispatch.h"
  28. #include "trapAvail.h"
  29.  
  30. /*    Private routine prototypes */
  31.  
  32. static void InitStuff(void);
  33. static void SetUpMenus(void);
  34. static void SetUpWindows(void);
  35. static void SetupRecordingFloater(void);
  36. static void SetupToolsFloater(void);
  37. static void SetupTestWindows(void);
  38. static void SetUpLimits(void);
  39. static void SetUpAppleEvents(void);
  40.  
  41. /*    Global variables    */
  42.  
  43. Boolean        gDone;                        /* Set to true if user selects Quit */
  44. Boolean     gInBackground;                /* Set to true if app is switched into background */
  45. short        gTimeToDialogDismissal;
  46. Rect        gDragArea;                    /* Area in which a window can be dragged */
  47. Rect        gGrowBounds;                /* Min and maximum size a window can be sized to */
  48. Boolean        gNotificationPosted = false;// True if a notification was posted
  49. NMRec        gNotificationRec;            // the notification record
  50. WindowRef    gRecordingFloater;            // Reference to recording floating window
  51. WindowRef    gToolsFloater;                // Reference to tools floating window
  52.  
  53. // UPPs
  54. AEEventHandlerUPP    gOAPPHandlerUPP, gODOCHandlerUPP, gPDOCHandlerUPP, gQUITHandlerUPP;
  55. ActivateHandlerUPP    gRecordingFloaterActivateHandler; // The UPPs for my windows
  56. ActivateHandlerUPP    gToolsFloaterActivateHandler;
  57. ActivateHandlerUPP    gActivateEventHandler;
  58.  
  59. ActivateHandlerUPP    gTestActivateEventHandler;
  60.  
  61.  
  62. void main()
  63. {
  64.     MaxApplZone();
  65.  
  66.     InitStuff();
  67.     EventLoop();
  68. }
  69.  
  70. void InitStuff()
  71. {
  72.     EventRecord    firstEvent;
  73.     OSErr        gestaltError;
  74.     long        gestaltResponse;
  75.     InitGraf((Ptr) &qd.thePort);
  76.     InitFonts();
  77.     InitWindows();
  78.     InitMenus();
  79.     InitDialogs(nil);
  80.     InitCursor();
  81.     
  82.     if (!TrapAvailable(_Gestalt))                                            /* Look for _Gestalt */
  83.         FatalErrorAlert(kSystemVersionTooOld);
  84.     
  85.     WaitNextEvent(everyEvent, &firstEvent, 60, nil);
  86.     FlushEvents(everyEvent, 0);
  87.     
  88.     /*    Check to see if certain features are implemented */
  89.     
  90.     gestaltError = Gestalt(gestaltAppleEventsAttr, &gestaltResponse);        /* Check for AppleEvents™ */
  91.     if (!gestaltError)
  92.         {
  93.             if ((gestaltResponse & (1 << gestaltAppleEventsPresent)) == 0)
  94.                 FatalErrorAlert(kNoAppleEvents);
  95.         }
  96.     else
  97.         FatalErrorAlert(kGestaltError);
  98.     
  99.     gestaltError = Gestalt(gestaltPPCToolboxAttr, &gestaltResponse);        /* Check for PPC Toolbox */
  100.     if (gestaltError)
  101.         FatalErrorAlert(kGestaltError);
  102.     
  103.     SetUpMenus();
  104.     SetUpWindows();
  105.     SetUpLimits();
  106.     SetUpAppleEvents();
  107. }
  108.  
  109.  
  110. /*    Get the menu bar from resource fork.  Fill  menu with DAs.  Draw it */
  111.  
  112. void SetUpMenus()
  113. {
  114.     Handle    theMenu;
  115.     
  116.     theMenu = GetNewMBar(rMbarID);
  117.     SetMenuBar(theMenu);
  118.     DisposHandle(theMenu);
  119.     AddResMenu(GetMHandle(rAppleMenuID),'DRVR');
  120.     DisableItem(GetMHandle(rEditMenuID), 0);
  121.     CheckItem(GetMHandle(rFloatersMenuID), kRecordingItem, true);
  122.     CheckItem(GetMHandle(rFloatersMenuID), kToolsItem, true);
  123.     
  124.     // For testing
  125.     if(Button())
  126.     {
  127.         MenuHandle        mhndl;
  128.         
  129.         //  Add the "Test" menu 
  130.         mhndl = GetMenu(132);
  131.         if(mhndl != nil)
  132.         {
  133.             (*mhndl)->menuID = 132;
  134.             InsertMenu(mhndl, 0);
  135.         }
  136.     }
  137.     
  138.     DrawMenuBar();
  139. }
  140.  
  141. /*
  142.     Set up any windows needed by the application
  143. */
  144.  
  145. void SetUpWindows()
  146. {
  147.     gActivateEventHandler = NewActivateHandlerProc((ProcPtr)ActivateEventHandler);
  148.  
  149.     SetupRecordingFloater();
  150.     SetupToolsFloater();
  151.     
  152.     // hold down mouse button for test mode
  153.     if(Button())
  154.         SetupTestWindows();
  155. }
  156.  
  157. void SetupRecordingFloater()
  158. {
  159.     Rect        theRect;
  160.     PicHandle    recordingControlsPicture;
  161.     OSErr        error;
  162.     
  163.     recordingControlsPicture = (PicHandle) GetPicture(128);
  164.     theRect = (**recordingControlsPicture).picFrame;
  165.     OffsetRect(&theRect, 70, 70);
  166.     
  167.     gRecordingFloaterActivateHandler = NewActivateHandlerProc((ProcPtr)RecordingFloaterActivateHandler);
  168.     error = NewWindowReference(&gRecordingFloater, &theRect, "\pRecord", true, 
  169.                     kHasPaletteTitlebarMask, (WindowRef) -1, 0, gRecordingFloaterActivateHandler);
  170.     SetWindowPic( gRecordingFloater, recordingControlsPicture);
  171. }
  172.  
  173. void SetupToolsFloater()
  174. {
  175.     Rect        theRect;
  176.     PicHandle    toolsPicture;
  177.     OSErr        error;
  178.     
  179.     toolsPicture = (PicHandle) GetPicture(130);
  180.     theRect = (**toolsPicture).picFrame;
  181.     OffsetRect(&theRect, 170, 100);
  182.     
  183.     gToolsFloaterActivateHandler = NewActivateHandlerProc((ProcPtr)ToolsFloaterActivateHandler);
  184.     error = NewWindowReference(&gToolsFloater, &theRect, "\pTools", true,
  185.                     kHasPaletteTitlebarMask, (WindowRef) -1, 0, gToolsFloaterActivateHandler);
  186.     SetWindowPic( gToolsFloater, toolsPicture);
  187. }
  188.  
  189. void SetupTestWindows()
  190. {
  191.     Rect        theRect;
  192.     OSErr        error;
  193.     WindowAttributes    attributes;
  194.     WindowRef    wind;
  195.     PicHandle    pict;
  196.     
  197.     // First a few floaters with various variation codes
  198.     gTestActivateEventHandler = NewActivateHandlerProc((ProcPtr)TestFloaterActivateHandler);
  199.     
  200.     pict = GetPicture(132);
  201.     if(pict == nil)
  202.         return;
  203.     
  204.     theRect = (**pict).picFrame;
  205.     ReleaseResource((Handle)pict);
  206.     pict = nil;
  207.     
  208.     OffsetRect(&theRect, 200, 140);
  209.     
  210.     // Make a few floaters with different attributes
  211.     
  212.     // a close box
  213.     attributes = kHasPaletteTitlebarMask + kHasCloseBoxMask;
  214.     error = NewWindowReference(&wind, &theRect, "\pYawk", true,
  215.                     attributes, (WindowRef) -1, 0, gTestActivateEventHandler);
  216.     
  217.     // add a zoom box
  218.     attributes += kHasZoomBoxMask;
  219.     OffsetRect(&theRect, 20, 20);
  220.     error = NewWindowReference(&wind, &theRect, "\pHibberly", true,
  221.                     attributes, (WindowRef) -1, 0, gTestActivateEventHandler);
  222.  
  223.     // add a grow box, no zoom box, side bar
  224.     attributes = attributes + kHasGrowBoxMask + kHasSideTitleBarMask - kHasZoomBoxMask;
  225.     OffsetRect(&theRect, 20, 20);
  226.     error = NewWindowReference(&wind, &theRect, "\pFrenality", true,
  227.                     attributes, (WindowRef) -1, 0, gTestActivateEventHandler);
  228.     
  229.     // Now some odd doc windows
  230.     attributes = kHasRoundedTitlebarMask + kHasCloseBoxMask;
  231.     OffsetRect(&theRect, -140, -100);
  232.     theRect.right += 50;
  233.     error = NewWindowReference(&wind, &theRect, "\pPiffle", true,
  234.                     attributes, (WindowRef) -1, 0, gActivateEventHandler);
  235.     
  236.     attributes = kHasRoundedTitlebarMask; // no close box
  237.     OffsetRect(&theRect, 20, 10);
  238.     theRect.right += 50;
  239.     error = NewWindowReference(&wind, &theRect, "\pWiffle", true,
  240.                     attributes, (WindowRef) -1, 0, gActivateEventHandler);
  241.     
  242.     // Try creating some windows from WIND resources
  243.     error = GetNewWindowReference(&wind, 129, (WindowRef) -1, gActivateEventHandler);
  244.     if(error == noErr)
  245.         ShowReferencedWindow(wind);
  246.  
  247.     error = GetNewWindowReference(&wind, 130, (WindowRef) -1, gActivateEventHandler);
  248.     if(error == noErr)
  249.         ShowReferencedWindow(wind);
  250.     
  251.     // a floater
  252.     error = GetNewWindowReference(&wind, 131, (WindowRef) -1, gTestActivateEventHandler);
  253.     if(error == noErr)
  254.         ShowReferencedWindow(wind);
  255. }
  256.  
  257. /*
  258.     Set up areas for the screen in which windows can be dragged and sized.
  259. */
  260.  
  261. void SetUpLimits()
  262. {
  263.     RgnHandle    deskTop;
  264.     
  265.     deskTop = GetGrayRgn();
  266.     gDragArea = (**deskTop).rgnBBox;
  267.     SetRect(&gGrowBounds, 48, 48,
  268.             ((gDragArea.right) - (gDragArea.left)),((gDragArea.bottom) - (gDragArea.top)));
  269. }
  270.  
  271. /*    Set up the AppleEvent dispatch table */
  272.  
  273. void SetUpAppleEvents()
  274. {
  275.     OSErr    installAppleEventError;
  276.     
  277.     // Install the required Apple Event Handlers
  278.     gOAPPHandlerUPP = NewAEEventHandlerProc(OAPPHandler);
  279.     gODOCHandlerUPP = NewAEEventHandlerProc(ODOCHandler);
  280.     gPDOCHandlerUPP = NewAEEventHandlerProc(PDOCHandler);
  281.     gQUITHandlerUPP = NewAEEventHandlerProc(QUITHandler);
  282.  
  283.     installAppleEventError = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, gOAPPHandlerUPP, 0, false);
  284.     installAppleEventError += AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, gODOCHandlerUPP, 0, false);
  285.     installAppleEventError += AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, gPDOCHandlerUPP, 0, false);
  286.     installAppleEventError += AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, gQUITHandlerUPP, 0, false);
  287.  
  288.     if (installAppleEventError)
  289.         FatalErrorAlert(kAEInstallError);
  290. }
  291.